#!/usr/bin/env python
from lib.format_time import *
from lib.HandleMysql import *
from lib.switch_code import *
from shop_conn import *
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')

def get_channel_data(date):
    
    print "get_user_action_data begin : %s"%date
    
    stat_conn = set_stats_conn()
    shop_conn = set_shop_conn()
    prod_conn = set_product_conn()
    conn = set_order_conn()
    stat_table = "merchant_action"

    #new shop
    query = "select id,shopname from shop where date(addtime)='%s'"%date
    items = shop_conn.get_results(query)
    shop_type = 1
    login_times=1
    credit_level=0
    for item in items:
        shop_id = item[0]
        shop_name = item[1]
        
        query1 = "select count(1) from t_product where shopid=%s and date(adddate)='%s'"%(shop_id,date)
        items1 = prod_conn.get_results(query1)
        prod_count = items1[0][0]
        
        sql_album = "select count(1) from album where shopid=%s and date(adddate)='%s'"%(shop_id,date)
        items_album = shop_conn.get_results(sql_album)
        album_count = items_album[0][0]
        
        sql_pic = "select count(1) from pic where state=0 and  shopid=%s and date(adddate)='%s'"%(shop_id,date)
        items_pic = shop_conn.get_results(sql_pic)
        pic_count = items_pic[0][0]   
        
        param = [date,shop_id,shop_name,shop_type,login_times,prod_count,album_count,pic_count,credit_level]
        stat_conn.insert_record(stat_table,param)
    
    query = "select shopid,count(1) from t_product where date(adddate)='%s' group by shopid"%date
    items = prod_conn.get_results(query)
    for item in items:
        shop_id = item[0]
        prod_count = item[1]
        
        query1 = "select count(1) from merchant_action where shop_id=%s and stat_date='%s' "%(shop_id,date)
        items1 = stat_conn.get_results(query1)
        cnt = items1[0][0]
        if cnt == 0 :
            query1 = "select shopname,date(addtime) from shop where id=%s"%shop_id
            items1 = shop_conn.get_results(query1)
            if len(items1)>0:
                shop_name = items1[0][0]
                add_date = items1[0][1]
                
                if add_date != date:
                    shop_type =2
                
                sql_album = "select count(1) from album where shopid=%s and date(adddate)='%s'"%(shop_id,date)
                items_album = shop_conn.get_results(sql_album)
                album_count = items_album[0][0]
                
                sql_pic = "select count(1) from pic where state=0 and  shopid=%s and date(adddate)='%s'"%(shop_id,date)
                items_pic = shop_conn.get_results(sql_pic)
                pic_count = items_pic[0][0]   
                
                param = [date,shop_id,shop_name,shop_type,login_times,prod_count,album_count,pic_count,credit_level]
                #print param
                stat_conn.insert_record(stat_table,param)
    stat_conn.close()
    shop_conn.close()
    prod_conn.close()
    conn.close()
    print "get_user_action_data end : %s"%date
#logic
def execute(date):
    # get main data
    # from order orderproduct orderticket orderpay orderlog
    get_channel_data(date)
    pass

def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()
